package com.erp.admin.business.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.base.base.BaseController;
import com.erp.base.base.Page;
import com.erp.base.business.entity.MemberConsultAnswer;
import com.erp.base.business.service.MemberConsultAnswerService;
import com.erp.base.common.utils.R;
/**
 * 会员与专家交流表
 * 
 * @author wanghw
 * @email whongwei_yz@sina.com
 * @date 2018-10-22 11:31:32
 */
 
@Controller
@RequestMapping("/business/memberConsultAnswer")
public class MemberConsultAnswerController extends BaseController{
	@Autowired
	private MemberConsultAnswerService memberConsultAnswerService;
	
	@GetMapping()
	@RequiresPermissions("business:memberConsultAnswer:memberConsultAnswer")
	String MemberConsultAnswer(){
	    return "business/memberConsultAnswer/memberConsultAnswer";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("business:MemberConsultAnswer:memberConsultAnswer")
	public Page<MemberConsultAnswer> list(MemberConsultAnswer memberConsultAnswer,HttpServletRequest request,HttpServletResponse response){
		//查询列表数据
		Page<MemberConsultAnswer> page= Page.bootstrapPage(request, response);
		page = memberConsultAnswerService.findPage(page, memberConsultAnswer);
		return page;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("business:memberConsultAnswer:add")
	String add(){
	    return "business/memberConsultAnswer/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("business:memberConsultAnswer:edit")
	String edit(@PathVariable("id") Long id,Model model){
		MemberConsultAnswer memberConsultAnswer = memberConsultAnswerService.get(id);
		model.addAttribute("memberConsultAnswer", memberConsultAnswer);
	    return "business/memberConsultAnswer/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("business:memberConsultAnswer:add")
	public R save( MemberConsultAnswer memberConsultAnswer,Model model){
		if(!beanValidator(model, memberConsultAnswer)){
			return R.error((String)model.asMap().get("message"));
		}
		try {
				memberConsultAnswerService.save(memberConsultAnswer);
				return R.ok();
			} catch (Exception e) {
				logger.error("保存失败", e);
				return R.error();
			}
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("business:memberConsultAnswer:edit")
	public R update( MemberConsultAnswer memberConsultAnswer,Model model){
		if(!beanValidator(model, memberConsultAnswer)){
			return R.error((String)model.asMap().get("message"));
		}
		try {
				memberConsultAnswerService.save(memberConsultAnswer);
				return R.ok();
			} catch (Exception e) {
				logger.error("保存失败", e);
				return R.error();
			}
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("business:memberConsultAnswer:remove")
	public R remove( Long id){
		try {
			memberConsultAnswerService.delete(id);
			return R.ok();
		} catch (Exception e) {
			logger.error("删除失败， id:"+ id, e);
			return R.error();
		}
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("business:memberConsultAnswer:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		try {
			memberConsultAnswerService.deleteBatch(Arrays.asList(ids));
			return R.ok();
		} catch (Exception e) {
			logger.error("删除失败，ids:"+ids, e);
			return R.error();
		}

	}
	
}
